<template>
  <div style="position: relative;">
    <cus-card :showTitle="false" style=" padding-top: 20px;">
      <Tabs type="card" @on-click="changeProd">
        <TabPane label="智能生成">
          <createRecipe :producel="produce"></createRecipe>
        </TabPane>
        <TabPane label="手动生成">
          <createRecipe ref="createRecipe" :producel="produce"></createRecipe>
        </TabPane>
      </Tabs>
    </cus-card>
    <Spin size="large" fix v-if="loading"> </Spin>
    <!-- 手工创建食谱 - 选择餐次 -->
    <chooseMealtimes ref="chooseMealtimes" @chooseMealsOk="chooseMealsOk"></chooseMealtimes>
  </div>
</template>
<script>
import {
  getboardrate,
  getLabourStrengthList,
  getStallInfo,
  unitInfo,
  judgeIsCrateHandaworkMeal
} from "@/api/business/dishesModule";
import mealExpenses from "./createHandmadeTwo.vue";
import chooseMealtimes from "./components/chooseMealtimes";
import createRecipe from "./components/createRecipe.vue";
export default {
  components: { chooseMealtimes, mealExpenses, createRecipe },
  data() {
    return {
      loading: false,
      produce: 0,
      form: {
        ovenClass: null,
        stallId: null,
        stallDescr: null,
        strength: null,
        startDate: "",
        stopDate: "",
        name: "",
        type: 0,
        dateString: "",
        handType: 0, // 0 代表只有三餐的手工食谱 1 代表带间餐夜餐等的手工食谱
        make_type: 0 //0代表智能生成食谱 1代表智能生成食谱
      },
      nameItem: {
        ovenClass: "",
        stall: "",
        name: ""
      },

      ovenClassList: [], // 伙食灶别列表
      labourStrengthList: [], // 劳动强度列表
      stallList: [], // 档口列表
      dateList: [],
      dateStr: "",
      pickerTime: {
        firstDayOfWeek: 1
      }
    };
  },
  computed: {},

  mounted() { },
  methods: {
    // 获取单位信息 --接口
    unitInfoApi() {
      return new Promise(resolve => {
        unitInfo({})
          .then(res => {
            this.form.loading = false;
            if (res.retCode == 2000) {
              this.nameItem.name = res.data[0].unitName || "";
            }
            resolve();
          })
          .catch(err => {
            reject();
          });
      });
    },
    changeProd(name) {
      console.log("----", name);
      this.produce = name;
    },
    // 获取伙食灶别列表 --接口
    getboardrateApi() {
      return new Promise(resolve => {
        getboardrate({
          boardRateType: 0
        })
          .then(res => {
            this.form.loading = false;
            if (res.retCode == 2000) {
              this.ovenClassList = res.data;
            }
            resolve();
          })
          .catch(err => {
            reject();
          });
      });
    },
    // 获取劳动强度列表 --接口
    getLabourStrengthListApi() {
      return new Promise(resolve => {
        getLabourStrengthList({})
          .then(res => {
            this.form.loading = false;
            if (res.retCode == 2000) {
              let arr = [];
              res.data.forEach(item => {
                arr.push({ labourId: item.labourId, strength: item.strength });
                if (item.activityTableList.length) {
                  item.activityTableList.forEach(it => {
                    arr.push({
                      labourId: it.labourId,
                      strength: it.activityName
                    });
                  });
                }
              });
              this.labourStrengthList = arr;
              console.log(this.labourStrengthList);
            }
            resolve();
          })
          .catch(err => {
            reject();
          });
      });
    },
    // 获取档口列表 --接口
    getStallInfoApi() {
      this.loading = true;
      return new Promise(resolve => {
        getStallInfo({})
          .then(res => {
            this.form.loading = false;

            if (res.retCode == 2000) {
              this.stallList = res.data;
              console.log("111");
            }

            resolve();
          })
          .catch(err => {
            reject();
          });
      });
    },
    //时间week事件
    weekChange(date) {
      this.getCurrentWeek(date);
      this.form.name = `${this.nameItem.stall +
        this.nameItem.name +
        this.nameItem.ovenClass +
        this.form.startDate}-${this.form.stopDate}号食谱`;
    },
    // 获取本周日期组件处理
    getCurrentWeek(date) {
      let nowData = new Date(date); //传入时间参数,如 2021-11-09 获取给定日期所属的周 日期
      let currentDay = nowData.getDay();
      if (currentDay == 0) {
        currentDay = 7;
      }
      let beforweek = [],
        afterWeek = [];
      for (let i = 1; i <= 7; i++) {
        if (currentDay - i >= 0) {
          let DayTime =
            nowData.getTime() - (currentDay - i) * 24 * 60 * 60 * 1000;
          beforweek.push(new Date(DayTime).Format("yyyy-MM-dd"));
        } else {
          let DayTime = nowData.getTime() + (8 - i) * 24 * 60 * 60 * 1000;
          afterWeek.push(new Date(DayTime).Format("yyyy-MM-dd"));
        }
      }
      afterWeek = afterWeek.reverse();
      this.dateList = [...beforweek, ...afterWeek];
      if (this.dateList.length) {
        this.dateStr = `${this.dateList[0]} - ${this.dateList[this.dateList.length - 1]
          }`;
        this.form.startDate = `${this.dateList[0]}`;
        this.form.stopDate = `${this.dateList[this.dateList.length - 1]}`;
      } else this.dateStr = "";
    },
    changeOvenClass(value) {
      this.nameItem.ovenClass = value.label;
      this.form.name = `${this.nameItem.stall +
        this.nameItem.name +
        value.label +
        this.form.startDate}-${this.form.stopDate}号食谱`;
    },
    changeStall(value) {
      this.nameItem.stall = value.label;
      let a = this.stallList.filter(item => item.id == value.value);
      if (a) {
        this.form.stallDescr = a[0].stallDescr;
      }
      this.form.name = `${value.label +
        this.nameItem.name +
        this.nameItem.ovenClass +
        this.form.startDate}-${this.form.stopDate}号食谱`;
    },
    // 点击创建
    // onAdd(value) {
    //     this.$refs["formValidate"].validate((valid) => {
    //         if (valid) {
    //             this.form.handType = value;
    //             judgeIsCrateHandaworkMeal({
    //                 beginDate: this.form.startDate,
    //                 endDate: this.form.stopDate,
    //                 mealType: 0
    //             }).then((res) => {
    //                 if (res.retCode == 2000) {
    //                     this.$Message.success("允许手工生成本周食谱, 正在前往生成...");
    //                     setTimeout(()=> {
    //                         this.$router.push({
    //                             name:'手工创建食谱',
    //                             query:{
    //                                 form: JSON.stringify(this.form)
    //                             }
    //                         })
    //                     }, 1500)

    //                 }
    //             });
    //         }
    //     });

    // },
    openChoose() {
      this.$refs["chooseMealtimes"].show();
    },
    // 选择餐次回调
    chooseMealsOk(arr) {
      this.form.handTypeArr = this.$cu.deepClone(arr);
      this.onAdd(1);
    }
  }
};
</script>

<style lang="less" scoped></style>
